<?php
/**
 * MySQL数据库操作
 * 
 * $Id: MySQL.class.php 219 2009-09-16 09:24:37Z sunxy $
 */ 

class MySQL {
	var $querynum = 0;
	
	/**
	 * private: resource link_identifier 
	 */
	var $dbh = 0;

	function connect($dbhost, $dbuser, $dbpw, $pconnect = false) {
		if ($pconnect) {
			$this->dbh = mysql_connect($dbhost, $dbuser, $dbpw);
		} else {
			$this->dbh = mysql_connect($dbhost, $dbuser, $dbpw);
		}
		if (!$this->dbh) {
			$this->halt("MySQL 数据库无法连接，请检查服务器或程序设置");
		}
	}

	function select_db($dbname) {
		return @mysql_select_db($dbname, $this->dbh);
	}

	function fetch_array($query, $result_type = MYSQL_ASSOC) {
		$query = @mysql_fetch_array($query, $result_type);
		return $query;
	}

	function query($sql, $silence = 0) {
		$query = @mysql_query($sql, $this->dbh);		
		if (!$query && !$silence) {
			if ($this->errno() == '1030') {
				@mysql_query("OPTIMIZE TABLE cdb_sessions", $this->dbh);				
				if (!$query && !$silence) {
					$this->halt("MySQL Query 错误", $sql);
				}
			}
		}
		$this->querynum++;
		return $query;
	}

	function limitQuery($sql, $from, $count) {
		$query = $this->query("$sql limit $from, $count");
		return $query;
	}

	function affected_rows() {
		return @mysql_affected_rows($this->dbh);
	}

	function error() {
		return @mysql_error($this->dbh);
	}

	function errno() {
		return @mysql_errno($this->dbh);
	}

	function result($query, $row) {
		$query = @mysql_result($query, $row);
		return $query;
	}

	function num_rows($query) {
		$query = @mysql_num_rows($query);
		return $query;
	}

	function num_fields($query) {
		return @mysql_num_fields($query);
	}

	function free_result($query) {
		return @mysql_free_result($query);
	}

	function insert_id() {
		$id = @mysql_insert_id($this->dbh);
		return $id;
	}

	function fetch_row($query) {
		$query = @mysql_fetch_row($query);
		return $query;
	}

	function close() {
		return @mysql_close($this->dbh);
	}

	function halt($message = "", $sql = "") {
		$timestamp = time();
		$errmsg = "";
		$cdbuser = $GLOBALS[HTTP_SESSION_VARS]["_cdbuser"] ? $GLOBALS[HTTP_SESSION_VARS]["_cdbuser"] : $GLOBALS[HTTP_COOKIE_VARS]["_cdbuser"];
		if ($message) {
			$errmsg = "提示: $message\n\n";
		}
		if ($cdbuser) {
			$errmsg .= "用户: $cdbuser\n";
		}
		$errmsg .= "时间: ".gmdate("Y-n-j g:ia", $timestamp + ($GLOBALS["timeoffset"] * 3600))."\n";
		$errmsg .= "程序: ".$GLOBALS["PHP_SELF"]."\n\n";
		if ($sql) {
			$errmsg .= "语句: ".htmlspecialchars($sql)."\n";
		}
		$dberror = $this->error();
		$dberrno = $this->errno();
		$errmsg .= "错误: $dberror\n";
		$errmsg .= "错误号: $dberrno";
		
		echo nl2br($errmsg);
		if($GLOBALS[adminemail]) {
			$errnos = array();
			$errorlogs = "";
			if ($errlog = @file("./dberror.log")) {
				for ($i = 0; $i < count($errlog); $i++) {
					$log = explode("\t", $errlog[$i]);
					if (($timestamp - $log[0]) > 86400) {
						$errlog[$i] = "";
					} else {
						$errnos[] = $log[1];
					}
				}
			}
			if (!in_array($dberrno, $errnos)) {
				$errorlogs .= "$timestamp\t$dberrno\n";
				echo "\n\n错误报告已经发送至系统Email信箱";
				@mail($GLOBALS[adminemail], "MySQL错误报告",
						"您的数据库错误, 详情如下\n\n".
						"$errmsg\n\n".
						"请检查数据库服务器或程序,24小时以内类似的错误将不再向您报告\n");
			} else {
				echo "\n\n同样的错误报告之前已经发送至系统Email信箱";
			}
			for ($i = 0; $i < count($errlog); $i++) {
				$errorlogs .= $errlog[$i] ? trim($errlog[$i])."\n" : NULL;
			}
			@$fp = fopen("./dberror.log", "w");
			@flock($fp, 3);
			@fwrite($fp, $errorlogs);
			@fclose($fp);
		}
		exit;
	}
	
	function fetch_all($query, $result_type = MYSQL_ASSOC) {
		$ret = null;
		while($row = $this->fetch_array($query, $result_type)) {
			$ret[] = $row;
		}
		
		return $ret;
	}	
}
?>